package cn.cybozu.api;

import java.net.HttpURLConnection;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

public class CybozuRest
{
	protected static DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
	protected static DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;
	protected static DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
	
	private String loginName         = null;
	private String password          = null;
	private String domain            = null;
	private String basicAuthName     = null;
	private String basicAuthPassword = null;
	private boolean debugMode        = false;
	
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	public String getDomain()
	{
		return domain;
	}
	public void setDomain(String domain)
	{
		this.domain = domain;
	}
	public String getLoginName()
	{
		return loginName;
	}
	public void setLoginName(String loginName)
	{
		this.loginName = loginName;
	}
	public String getBasicAuthName()
	{
		return basicAuthName;
	}
	public void setBasicAuthName(String basicAuthName)
	{
		this.basicAuthName = basicAuthName;
	}
	public String getBasicAuthPassword()
	{
		return basicAuthPassword;
	}
	public void setBasicAuthPassword(String basicAuthPassword)
	{
		this.basicAuthPassword = basicAuthPassword;
	}
	public String getUrl()
	{
		return "https://" + domain;
	}
	public boolean isDebugMode()
	{
		return debugMode;
	}
	public void setDebugMode(boolean debugMode)
	{
		this.debugMode = debugMode;
	}
	protected void setRequestHeader(HttpURLConnection urlconn, String apiToken)
	{
		if(apiToken == null)
		{
			String countPassStr = getLoginName() + ":" + getPassword();
			urlconn.setRequestProperty("X-Cybozu-Authorization", Base64.getEncoder().encodeToString(countPassStr.getBytes()));
		}
		else
		{
			urlconn.setRequestProperty("X-Cybozu-API-Token", apiToken);
		}
	}
	
	
	/*protected void  setBasicAuth(DefaultHttpClient client)
	{
		if(basicAuthName != null)
		{
			Credentials credentials = new UsernamePasswordCredentials(getBasicAuthName(), getBasicAuthPassword());
			AuthScope scope = new AuthScope(getUrl(), 443);
			client.getCredentialsProvider().setCredentials(scope, credentials);
		}
	}*/
}
